<template>
  <div class="login-container">
    <!--
      el-form 表单组件
      每个表单项都必须使用 el-form-item 组件包裹
     -->
    <div class="login-form-wrap">
      <div class="login-head">
        <div class="logo"></div>
      </div>
      <el-form class="login-form" :model="form" :rules="rules" ref="form">
        <el-form-item prop="mobile">
          <el-input
            v-model="form.mobile"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            v-model="form.code"
            placeholder="请输入验证码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="isAgree">
          <el-checkbox v-model="form.isAgree">我已阅读并同意用户协议和隐私条款</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button
            v-loading="loading"
            class="login-btn"
            type="primary"
            @click="onSubmit('form')"
          >登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      form: {
        mobile: '',
        code: '',
        isAgree: false
      },
      isLoding: false,
      loading: false,
      rules: {
        mobile: [
          // 账号的验证规则:值改变化时触发
          { required: true, message: '必须输入手机号', trigger: 'blur' },
          { pattern: /^1[35789]\d{9}$/, message: '手机号格式不对', trigger: 'change' }
        ],
        code: [
          // 密码验证
          { required: true, message: '必须要输入验证码', trigger: 'blur' },
          { pattern: /^\d{6}$/, message: '验证码格式不对', trigger: 'change' }
        ],
        isAgree: [
          {
            // value: 表示当前值
            // 验证通过： 直接写callback()
            // 验证不通过： callback(new Error(‘错误消息))
            validator: (rule, value, callback) => {
              if (value) {
                // 如是选中，则验证通过
                callback()
              } else {
                // 如是不选中，则给出错误提示通过
                callback(new Error('请同意用户协议'))
              }
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    onSubmit (formName) {
      this.loading = true
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.login()
          this.loading = false
        } else {
          this.loading = false
        }
      })
    },
    async login () {
      try {
        const res = await this.$http({
          method: 'POST',
          url: '/mp/v1_0/authorizations',
          data: {
            mobile: this.form.mobile,
            code: this.form.code
          }
        })
        console.log(res.data.data)
        localStorage.setItem('info', JSON.stringify(res.data.data))
        // const obj = localStorage.getItem('info')
        // console.log(obj)
        this.loading = false
        this.$message({
          message: '登录成功',
          type: 'success'
        })
        this.$router.push('/')
      } catch (err) {
        console.log(2)
        console.log(err)
        this.isLoading = false
        this.$message({
          message: '登录失败重新登录',
          type: 'error'
        })
      }
    }

  }
}
</script>
<style lang="less" scoped>
// 铺满整屏
.login-container {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction:column;
  justify-content: center;
  align-items: center;
  background: url("../../assets/login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    min-width: 300px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("../../assets/logo_index.png") no-repeat;
        background-size: contain;
      }
    }
    .login-form {
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
